package wortubung.lang;

import java.util.Collection;

import wortubung.core.LANG;
import wortubung.inflection.VerbConjugation;
import wortubung.inflection.external.FrenchConjugationByReversoNet;
import wortubung.lang.fr.Conjugueur;
import wortubung.util.Printable;
import wortubung.util.TransliterateException;

import static wortubung.util.LatinScript.*;
import static wortubung.util.CommonUtils.*;

/**
 * Singleton
 */
public class LangFrenchManager extends LanguageManager {

	LangFrenchManager(Printable prompt) {
		super(prompt, new FrenchConjugationByReversoNet());

		// pre-conjugate encoded irregular verbs (roots)
		Collection<String> embeddedVerbs = getVerbsOfIrregularConjugations();
    	for (String verb : embeddedVerbs)
   			preconjugate(verb);

    	// pre-conjugate more verbs
    	try {
			String str = readFileFromCPAsString(FR_PRECONJUGUES_FILE);
			String[] sa = str.split("\n");
			trim(sa);
			for (String s : sa) {
				if (isNotEmpty(s) && !embeddedVerbs.contains(s))
					preconjugate(enAccent(s));
			}
    	} catch(Exception e) {
    		e.printStackTrace();
    	}
	}

	@Override public final LANG getLanguage() { return LANG.fr; }

    @Override protected String preprocessWordPattern(String pattern) throws TransliterateException {
    	pattern = toLatin(toVernacular(pattern), false);
   		if (pattern.endsWith("e"))
   			pattern = chopRight(pattern, 1) + "(e?)";
   		else if (pattern.endsWith("eaux") || pattern.endsWith("oux") || pattern.endsWith("eux"))
   			pattern = chopRight(pattern, 1) + "(x?)";
    	return pattern.replaceAll("_", " ").replaceAll("\\*", ".*");
    }

	@Override public VerbConjugation conjugate(String verb) {
		return Conjugueur.conjugate(verb);
	}

	@Override public Collection<String> getVerbsOfIrregularConjugations() {
		return Conjugueur.defaultConjugator.getVerbsOfIrregularConjugations();
	}

    @Override
    public String numberInWords(int integral, String fraction, boolean isOrdinal) {
    	// TODO: impl this!
    	return null;
    }

}
